<template>
	<view class="repair-wrap">
		<view style="border-top: 1px solid #F7F8F9;background-color: #FFFFFF;margin-top: 10px;padding-bottom: 10px;">
			<view style="width: 88%;margin:auto;">
				<view class="header-li">
					<view style="width: 98px;">
						<view>
							<text style="color: red;">*</text>
							车牌号
						</view>
					</view>
					<view class="input1">
						<u-input v-model="ruleFrom.driving_number" maxlength="8" style="margin-top: 7px;" placeholder="请输入" />
					</view>
				</view>
				<view class="header-li">
					<view style="width: 98px;">
						<view>
							<text style="color: red;">*</text>电话号码
						</view>
					</view>
					<view class="input1">
						<u-input v-model="ruleFrom.driving_tel" type="number" maxlength="11" style="margin-top: 7px;" placeholder="请输入" />
					</view>
				</view>
				<view class="header-li">
					<view style="width: 98px;">
						<view>
							<text style="color: red;">*</text>姓名
						</view>
					</view>
					<view class="input1">
						<u-input v-model="ruleFrom.driving_name" maxlength="4" style="margin-top: 7px;" placeholder="请输入"  />
					</view>
				</view>
				<view class="header-li">
					<view style="width: 98px;">
						<view>
							<text style="color: red;">*</text>品牌
						</view>
					</view>
					<view class="input1">
						<u-input v-model="ruleFrom.brand" style="margin-top: 7px;" placeholder="请输入" />
					</view>
				</view>
				<view class="header-li">
					<view style="width: 98px;">
						<view>
							<text style="color: red;">*</text>型号
						</view>
					</view>
					<view class="input1">
						<u-input v-model="ruleFrom.driving_model" style="margin-top: 7px;" placeholder="请输入" />
					</view>
				</view>
				<view class="header-li">
					<view style="width: 98px;">
						<view>
							<text style="color: red;">*</text>颜色
						</view>
					</view>
					<view class="input1">
						<u-input v-model="ruleFrom.driving_color" style="margin-top: 7px;" placeholder="请输入" />
					</view>
				</view>
				<view style="border-bottom: 1px solid #EEEEEEFF;padding-bottom: 10px;">
					<view class="guarn-box u-m-t-60">
						<view class="header-name">
							<text style="color: red;">*</text>行驶证
						</view>
						<view style="display: flex;">
							<view class="guarn-box guarn-box2 u-m-t-20" v-if="ruleFrom.driving_license_image">
								<view class="guarn-box2-img">
									<image mode="aspectFill" :src="ruleFrom.driving_license_image" @click="preview(ruleFrom.driving_license_image)" style="border-radius: 20rpx;"></image>
									<u-icon @click="colseImg(ruleFrom.driving_license_image)" name="close" color="red"
										size="40" class="guarn-box2-img-close"></u-icon>
								</view>
							</view>
							<view class="guarn-box u-m-t-20" v-show="!ruleFrom.driving_license_image">
								<view @click="uoloadHouse" class="guarn-box-img">
									<image class="u-m-t-72" src="https://clhy-img.oss-cn-beijing.aliyuncs.com/ownerImage/camera.svg" mode=""></image>
								</view>
							</view>
						</view>
					</view>
					<view style="margin-top: 16px;"><span style="color: #999999FF;">最多上传1张图片</span></view>
				</view>
				<view style="border-bottom: 1px solid #EEEEEEFF;padding-bottom: 10px;">
					<view class="guarn-box u-m-t-60">
						<view class="header-name">
							<text style="color: red;opacity: 0;">*</text>车险
						</view>
						<view style="display: flex;">
							<view class="guarn-box guarn-box2 u-m-t-20" v-if="ruleFrom.driving_autoinsurance_image">
								<view class="guarn-box2-img">
									<image mode="aspectFill" style="border-radius: 20rpx;" @click="preview(ruleFrom.driving_autoinsurance_image)" :src="ruleFrom.driving_autoinsurance_image">
									</image>
									<u-icon @click="colseImg1(ruleFrom.driving_autoinsurance_image)" name="close"
										color="red" size="40" class="guarn-box2-img-close"></u-icon>
								</view>
							</view>
							<view class="guarn-box u-m-t-20" v-show="!ruleFrom.driving_autoinsurance_image">
								<view @click="uoloadHouse1" class="guarn-box-img">
									<image class="u-m-t-72" src="https://clhy-img.oss-cn-beijing.aliyuncs.com/ownerImage/camera.svg" mode=""></image>
								</view>
							</view>
						</view>
					</view>
					<view style="margin-top: 16px;"><span style="color: #999999FF;">最多上传1张图片</span></view>
				</view>
			</view>
			<view style="margin-top: 30px;padding-bottom: 20px;">
				<button class="yilingqu" @click="sumbitAdd">确认增加车辆信息</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		pathToBase64,
		base64ToPath
	} from '@/utils/image-tool.js'
	import LbPicker from '@/components/lb-picker'
	export default {
		data() {
			return {
				type: 1,
				index: 0,
				array: ['分类', '分类二'],
				inday: 0,
				ruleForm: {
					renType: null,
				},
				indexList: true,
				ruleFrom: {
					user_id: null, //操作人id
					room_id: null, //房间id
					driving_number: null, //车牌号
					driving_tel: null, //电话号码
					driving_name: null, //姓名
					brand: null, //品牌
					driving_color: null, //颜色
					driving_model: null, //型号
					driving_license_image: null, //行驶证
					driving_autoinsurance_image: null, //车险
				}
			}
		},
		onShow() {
			this.homelist = uni.getStorageSync('hourseList')[uni.getStorageSync('hourseNum') || 0]
			console.log(this.homelist)
		},
		methods: {
			preview(imgarrs){
				var imgarr = []
				imgarr.push(imgarrs)
				uni.previewImage({
					urls:imgarr,
				})
			},
			//车牌号
			yzCph(){
				let zm =/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
				if(this.ruleFrom.driving_number.search(zm) ==-1) {
					return false;
				}else{
					return true;
				}
			},
			//禁止输入特殊字符
			validateInput(){
				var regex = /^[a-zA-Z0-9\u4e00-\u9fa5]+$/; // 只允许英文、数字和汉字
				if (regex.test(this.ruleFrom.driving_name)) {
						console.log("输入合法");
						return true;
				} else {
					console.log("输入非法");
					return false;
				}
			},
			//增加车辆信息
			sumbitAdd() {
				this.ruleFrom.room_id = this.homelist.roomId
				this.ruleFrom.user_id = this.homelist.ownerUserId
				if (!this.ruleFrom.driving_number) {
					return uni.showToast({
						title: '请输入车牌号',
						icon: 'none'
					});
				}else{
					 if(!this.yzCph()){
						 return uni.showToast({
						 	title: '请输入正确的车牌号',
						 	icon: 'none'
						 });
					 }
				}

				if (!this.ruleFrom.driving_tel || this.ruleFrom.driving_tel.length < 11) {
					return uni.showToast({
						title: '请输入正确的电话号码',
						icon: 'none'
					});
				}
				if (!this.ruleFrom.driving_name || this.ruleFrom.driving_name.length <2) {
					return uni.showToast({
						title: '请输入正确的姓名',
						icon: 'none'
					});
				}else{
					if(!this.validateInput()){
						 return uni.showToast({
							title: '请输入正确的姓名',
							icon: 'none'
						 });
					}
				}
				if (!this.ruleFrom.brand) {
					return uni.showToast({
						title: '请输入品牌',
						icon: 'none'
					});
				}
				if (!this.ruleFrom.driving_model) {
					return uni.showToast({
						title: '请输入型号',
						icon: 'none'
					});
				}
				if (!this.ruleFrom.driving_color) {
					return uni.showToast({
						title: '请输入颜色',
						icon: 'none'
					});
				}
				if (!this.ruleFrom.driving_license_image) {
					return uni.showToast({
						title: '请上传行驶证照片',
						icon: 'none'
					});
				}

			//	return;
				this.$request.api.getvehicle(this.ruleFrom).then(res => {
					if (res.data.code == 0) {
						uni.showToast({
							title: '增加车辆信息成功！',
							icon: 'success',
							duration:2000
						});
						setTimeout(function() {
							uni.redirectTo({
								url: '/my/information/vehicleinformationList'
							})
						}, 2000)

						this.ruleFrom.room_id = null; //房间号
						this.ruleFrom.user_id = null; //上传人id
						this.ruleFrom.driving_number = null; //车牌号
						this.ruleFrom.driving_tel = null; //电话号码
						this.ruleFrom.driving_name = null; //姓名
						this.ruleFrom.brand = null; //品牌
						this.ruleFrom.driving_color = null; //颜色
						this.ruleFrom.driving_model = null; //型号
						this.ruleFrom.driving_license_image = null; //行驶证
						this.ruleFrom.driving_autoinsurance_image = null; //车险

					} else {
						uni.showToast({
							title: '新增车辆信息失败！',
							icon: 'error'
						});
					}
				})
			},
			checked(index) {
				this.isActive = index
			},
			newly() {
				uni.navigateTo({
					url: '/privateMsg/clap/homepageClap'
				})
			},
			shouList() {
				this.indexList = !this.indexList
			},
			//行驶证
			colseImg(val) {
				this.ruleFrom.driving_license_image = null;
				if (!this.ruleFrom.driving_license_image) {
					this.imgFlag = true
				}
			},
			// 选择图片
			uoloadHouse() {
				let _this = this;
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['camera '],
					success: function(res) {
						pathToBase64(res.tempFilePaths[0]).then(res => {
							_this.$request.api.image({
								imageBase64: res
							}).then(res => {
								_this.ruleFrom.driving_license_image =
									res.data.data
								_this.imgFlag = false;
							})
						})
					}

				})

			},
			//车险
			colseImg1(val) {
				this.ruleFrom.driving_autoinsurance_image = null;
				if (!this.ruleFrom.driving_autoinsurance_image) {
					this.imgFlag = true
				}
			},
			// 选择图片
			uoloadHouse1() {
				console.log("正在上传图片");
				let _this=this
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['camera '],
					success: function(res) {
						pathToBase64(res.tempFilePaths[0]).then(res => {
							_this.$request.api.image({
								imageBase64: res
							}).then(res => {
								_this.ruleFrom.driving_autoinsurance_image = res.data.data
								_this.imgFlag = false;
							})
						})
					}
				})
			},
		}
	}
</script>


<style lang="scss">
	.repair-wrap {
		width: 100%;
		min-height: 100vh;
		height: auto;
		background: #f4f4f4;
		overflow: hidden;

		.repair-box {
			background: #fff;

			.repair-titel {
				height: 44rpx;
				line-height: 44rpx;

				.repair-titel-l {
					float: left;
				}

				.repair-titel-r {
					float: right;
					color: #999;
				}
			}

			.repair-6 {
				color: #666;
			}
		}
	}

	.header-li {
		display: flex;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		line-height: 102rpx;
		color: #333333;
		border-bottom: 1rpx solid #EEEEEE;
	}

	.header-li1 {
		display: flex;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		margin-top: 20px;
		color: #333333;
	}

	.header-name {
		width: 186rpx;
	}

	.form-select {
		width: 480rpx;
		background: url(https://clhy-img.oss-cn-beijing.aliyuncs.com/ownerImage/right.png) no-repeat center right;
		background-size: 10rpx 17rpx;
	}

	.yilingqu {
		width: 80%;
		height: 84rpx;
		background: linear-gradient(180deg, #76D904 0%, #417505 100%);
		border-radius: 44rpx;
		border-radius: 44rpx;
		line-height: 84rpx;
		color: #FFFFFF;
		text-align: center;
		font-size: 32rpx;
	}

	.guarn-box {
		width: 100%;
		// height: 216rpx;
	}

	.guarn-box2 {
		display: flex;
	}

	.guarn-box2-img {
		width: 216rpx;
		height: 216rpx;
		margin-right: 20rpx;
		position: relative;

		.guarn-box2-img-close {
			position: absolute;
			top: 20rpx;
			right: 20rpx;
		}
	}

	.guarn-box2 image {
		width: 216rpx;
		height: 216rpx;
	}

	.guarn-box-img {
		width: 216rpx;
		height: 216rpx;
		float: left;
		margin-right: 24rpx;
		text-align: center;
		border: 2rpx solid #ccc;
		border-radius: 4rpx;
	}

	.guarn-box-img image {
		width: 76rpx;
		height: 68rpx;
	}
</style>